home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-09-04 | 3.2 KB | 139 lines | [TEXT/CWIE] |
- /*
- CPasswordField.cp
-
- Subclass of LEditField that is used for entering passwords in such a way
- that they can't be seen on screen.
-
- It doesn't do any sophisticated editing, if you press the delete key, the
- whole field is wiped out.
-
- History:
- Wed, 30 Aug 1995 Peter Marks Created.
-
- */
-
- #include "CPasswordField.h"
- #include "Utilities.h"
-
-
- CPasswordField::CPasswordField()
- {
- }
-
- CPasswordField::CPasswordField(LStream *inStream)
- : LEditField(inStream)
- {
- mContent[0] = 0;
- }
-
- CPasswordField::~CPasswordField()
- {
- }
-
- #pragma segment Main
- LEditField* CPasswordField::CreatePasswordFieldStream(LStream *inStream)// Override
- {
- return (new CPasswordField(inStream));
- }
-
-
- #pragma segment Main
- StringPtr CPasswordField::GetDescriptor(Str255 outDescriptor) const// Override
- {
- return pStrcpy(outDescriptor, (unsigned char *)mContent);
- }
-
-
- #pragma segment Main
- void CPasswordField::SetDescriptor(
- ConstStr255Param inDescriptor)// Override
- {
- ::pStrcpy(mContent, (unsigned char *)inDescriptor);
- short length = mContent[0];
- Str255 visibleString;
- visibleString[0] = '\0';
- for(short i = 0; i < length; i++)
- {
- pStrcat(visibleString, "\p•");
- }
- LEditField::SetDescriptor(visibleString);
- }
-
-
- #pragma segment Main
- void CPasswordField::FindCommandStatus(
- CommandT inCommand,
- Boolean &outEnabled,
- Boolean &outUsesMark,
- Char16 &outMark,
- Str255 outName)// Override
- {
- // disable cut and copy so they can't cheat
- switch(inCommand)
- {
- case cmd_Cut:
- case cmd_Copy:
- outEnabled = false;
- break;
- default:
- LEditField::FindCommandStatus(inCommand, outEnabled, outUsesMark, outMark, outName);
- break;
- }
- }
-
-
- #pragma segment Main
- Boolean CPasswordField::HandleKeyPress(
- const EventRecord &inKeyEvent)// Override
- {
- // append the character to the mContent buffer
- // handle delete key reasonably
- // put bullets in the visible field for feedback
- char theChar = inKeyEvent.message & charCodeMask;
- EventRecord myKeyEvent = inKeyEvent;
- // let return tab and enter pass through
- if((theChar != 0x0d) && (theChar != 0x03) && (theChar != '\t'))
- {
- if(theChar == '\b') // delete character
- {
- mContent[0] = 0; // clear the internal buffer
- SelectAll(); // and clear the visible field
- ::TEDelete(mTextEditH);
- return true;
- }
- else
- {
- // there are only two possible selection states, in this simple model
- // none or all
- // Check for any selection, otherwise clear the field when they type
- if((*mTextEditH)->selStart != (*mTextEditH)->selEnd)
- {
- mContent[0] = 1; // clear the internal buffer and add the character
- mContent[1] = theChar;
-
- SelectAll(); // and clear the visible field
- ::TEDelete(mTextEditH);
- ::TEKey('•', mTextEditH);
- }
- else
- {
- // ok, so append it to our internal buffer
- Str255 charStr;
- charStr[0] = 1;
- charStr[1] = theChar;
- ::pStrcat(mContent, charStr);
- ::TEKey('•', mTextEditH);
- }
- return true;
- }
- }
- return LEditField::HandleKeyPress(myKeyEvent);
- }
-
- // override to simply select all if they click in the field
- void CPasswordField::ClickSelf(const SMouseDownEvent & /* inMouseDown */)
- {
- ::TESetSelect(0, 32000, mTextEditH);
- SwitchTarget(this);
- }
-